package com.fingard.dsp.bank.reqrespobj.unifiedorder;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.RequestBase;
import com.fingard.text.StringHelper;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

public class ReqUnifiedOrder extends RequestBase {
    /**
     * 订单类型：QR-扫码
     */
    public String OrderType = "";

    /**
     * 公众号或开发者应用ID
     */
    public String appId = "";

    /**
     * 商户账号
     */
    public String ownAct = "";

    /**
     * 商户ID
     */
    public String merchantId = "";

    /**
     * 订单号
     */
    public String rdSeq;

    /**
     * 商品描述
     */
    public String description = "";

    /**
     * 交易币种
     */
    public String cur = "";

    /**
     * 交易金额
     */
    public String amount = "";

    /**
     * 商品备注
     */
    public String memo = "";

    /**
     * 扫码付模式，1-模式一 2-模式二
     */
    public String qrMode = "";

    /**
     * 订单生效时间
     */
    public String timeStart = "";

    /**
     * 支付机构代码
     */
    public String paymentInstitution = "";

    /**
     * 组织机构号（门店编号）
     */
    public String orgId = "";

    public Date getTimeStart() throws ParseException {
        return Format.DateTime14Format.parse(timeStart);
    }

    /**
     * 订单失效时间
     */
    public String timeExpire = "";

    public Date getTimeExpire() throws ParseException {
        return Format.DateTime14Format.parse(timeExpire);
    }

    /**
     * 获取有效时间 (分钟)
     *
     * @return 有效时间
     * @throws ParseException 日期格式转换异常
     */
    public String getValidTime() throws ParseException {
        String validTime = "";

        long timeOut = getTimeExpire().getTime() - getTimeStart().getTime();
        validTime = String.valueOf(timeOut / 1000 / 60);

        return validTime;
    }

    /**
     * 商品ID
     */
    public String productId = "";

    /**
     * 用户标识,trade_type=JSAPI时（即公众号支付），此参数必传
     */
    public String openid = "";

    /**
     * 关注公众号A的用公众号B收款时，需要通过code换取网页授权access_token
     * code通过GenGetUserCodeURL()返回的地址获得
     */
    public String code = "";

    /***/
    public String clientIp = "";

    public String attach = "";


    /**
     * 付款人姓名
     */
    public String oppActName = "";
    /**
     * 付方卡号
     */
    public String oppAct = "";
    /**
     * 证件类型
     */
    public String certType = "";

    /**
     * 付款人证件号码
     */
    public String certNumber = "";

    /**
     * 付款人手机号
     */
    public String cellPhone = "";

    /**
     * 投保地区编码
     */
    public String insureAreaCode;

    /**
     * 上海信息平台交易码
     */
    public String sHTradeNo;

    /**
     * 保险地区
     */
    public String region;

    /**
     * 保险平台交易号
     */
    public String circPaymentNo;

    /**
     * 可用渠道
     */
    public String enablePayChannels;

    /**
     * 支付宝用户id
     */
    public String alipayUid;

    /**
     * 保险交易号
     */
    public String paymentNo;

    /**
     * 实名标志(1:是,0:否)
     */
    public String realNameFlag;

    /**
     * 微信H5跳转URL
     */
    public String redirectUrl;

    /* 支付号 */
    public String policyid = "";

    /**
     * 保险公司代码
     */
    public String corpNo;

    /**
     * 实名类型(0-全国车险、1-上海车险、2-深圳车险、3-江苏车险)
     */
    public String realNameType;

    /**
     * 保单号
     */
    public String sourceNoteCode;

    /**
     * 投保单号
     */
    public String insuPolicyNum = "";

    /**
     * 用户ID
     */
    public String userId = "";

    /**
     * 签约类型(枚举：01-公众号,02-H5,03-扫码,04-APP,05-小程序)
     */
    public String signType = "";

    /**
     * 协议模板id
     */
    public String planId = "";

    /**
     * 签约协议号
     */
    public String authCode = "";
    /**
     * 支付方式
     */
    public String channel = "";
    /**
     * 支付方式
     */
    public String bankCode = "";

    /**
     * 请求序列号
     */
    public String serialNo = "";

    /**
     * 系统标志
     */
    public String systemFlag = "";

    /**
     * 是否需要拼接returnurl地址(0-不需要,其他-需要)
     */
    public boolean needReturnURL = true;

    /**
     * 定位码
     */
    public String alignCode = "";

    /**
     * 终端号
     */
    public String terid = "";

    /**
     * 是否原生
     */
    public String isRaw;

    /**
     * 是否允许使用信用卡(0-允许使用信用卡,1-禁用使用信用卡)
     */
    public String isLimitCredit;

    public ReqUnifiedOrder(HashMap<String, String> p_hashMap,
                           ReqRespHeader p_header) throws Exception {
        reqHeader = p_header;

        if (reqHeader.transType.equals("SubmitQROrder")) {
            OrderType = "QR";
        }

        if (p_hashMap.containsKey("AppId")) {
            appId = p_hashMap.get("AppId");
            if (appId == null) {
                appId = "";
            }
        }

        if (p_hashMap.containsKey("OurBankAccount")) {
            ownAct = p_hashMap.get("OurBankAccount");
            if (ownAct == null) {
                ownAct = "";
            }
        }

        if (p_hashMap.containsKey("MchId")) {
            merchantId = p_hashMap.get("MchId");
            if (merchantId == null) {
                merchantId = "";
            }
        }

        if (p_hashMap.containsKey("MerchantId")) {
            merchantId = p_hashMap.get("MerchantId");
            if (merchantId == null) {
                merchantId = "";
            }
        }

        if (p_hashMap.containsKey("RdSeq")) {
            rdSeq = p_hashMap.get("RdSeq");
        }

        if (p_hashMap.containsKey("Description")) {
            description = p_hashMap.get("Description");
        }

        if (p_hashMap.containsKey("Memo")) {
            memo = p_hashMap.get("Memo");
        }

        if (p_hashMap.containsKey("Cur")) {
            cur = p_hashMap.get("Cur");
        }

        if (p_hashMap.containsKey("Amount")) {
            amount = p_hashMap.get("Amount");
        }

        if (p_hashMap.containsKey("ProductId")) {
            productId = p_hashMap.get("ProductId");
        }

        if (p_hashMap.containsKey("OpenId")) {
            openid = p_hashMap.get("OpenId");
        }

        if (p_hashMap.containsKey("ClientIp")) {
            clientIp = p_hashMap.get("ClientIp");
        }

        if (p_hashMap.containsKey("TimeStart")) {
            timeStart = p_hashMap.get("TimeStart");
        }

        if (p_hashMap.containsKey("TimeExpire")) {
            timeExpire = p_hashMap.get("TimeExpire");
        }

        if (p_hashMap.containsKey("QRMode")) {
            qrMode = p_hashMap.get("QRMode");
        }

        if (p_hashMap.containsKey("Code")) {
            code = p_hashMap.get("Code");
        }

        if (p_hashMap.containsKey("Attach")) {
            attach = p_hashMap.get("Attach");
        }

        if (p_hashMap.containsKey("OppActName")) {
            oppActName = p_hashMap.get("OppActName");
        }
        if (p_hashMap.containsKey("OppAct")) {
            oppAct = p_hashMap.get("OppAct");
        }

        if (p_hashMap.containsKey("CertType")) {
            certType = p_hashMap.get("CertType");
        }

        if (p_hashMap.containsKey("CertNumber")) {
            certNumber = p_hashMap.get("CertNumber");
        }

        if (p_hashMap.containsKey("CellPhone")) {
            cellPhone = p_hashMap.get("CellPhone");
        }
        if (p_hashMap.containsKey("InsureAreaCode")) {
            insureAreaCode = p_hashMap.get("InsureAreaCode");
        }
        if (p_hashMap.containsKey("SHTradeNo")) {
            sHTradeNo = p_hashMap.get("SHTradeNo");
        }
        if (p_hashMap.containsKey("Region")) {
            region = p_hashMap.get("Region");
        }
        if (p_hashMap.containsKey("CircPaymentNo")) {
            circPaymentNo = p_hashMap.get("CircPaymentNo");
        }
        if (p_hashMap.containsKey("EnablePayChannels")) {
            enablePayChannels = p_hashMap.get("EnablePayChannels");
        }
        if (p_hashMap.containsKey("AlipayUid")) {
            alipayUid = p_hashMap.get("AlipayUid");
        }
        if (p_hashMap.containsKey("PaymentNo")) {
            paymentNo = p_hashMap.get("PaymentNo");
        }
        if (p_hashMap.containsKey("RealNameFlag")) {
            realNameFlag = p_hashMap.get("RealNameFlag");
        }
        if (p_hashMap.containsKey("RedirectUrl")) {
            redirectUrl = p_hashMap.get("RedirectUrl");
        }
        if (p_hashMap.containsKey("Policyid")) {
            policyid = p_hashMap.get("Policyid");
        }
        if (p_hashMap.containsKey("CorpNo")) {
            corpNo = p_hashMap.get("CorpNo");
        }
        if (p_hashMap.containsKey("RealNameType")) {
            realNameType = p_hashMap.get("RealNameType");
        }
        if (p_hashMap.containsKey("SourceNoteCode")) {
            sourceNoteCode = p_hashMap.get("SourceNoteCode");
        }
        if (p_hashMap.containsKey("InsuPolicyNum")) {
            insuPolicyNum = p_hashMap.get("InsuPolicyNum");
        }
        if (p_hashMap.containsKey("UserId")) {
            userId = p_hashMap.get("UserId");
        }
        if (StringHelper.isNullOrEmpty(userId)) {
            userId = p_hashMap.get("AlipayUid");
        }
        if (p_hashMap.containsKey("EntrustType")) {
            signType = p_hashMap.get("EntrustType");
        }
        if (p_hashMap.containsKey("PlanID")) {
            planId = p_hashMap.get("PlanID");
        }
        if (p_hashMap.containsKey("ContractCode")) {
            authCode = p_hashMap.get("ContractCode");
        }
        if (p_hashMap.containsKey("Channel")) {
            channel = p_hashMap.get("Channel");
        }
        if (p_hashMap.containsKey("OppBankCode")) {
            bankCode = p_hashMap.get("OppBankCode");
        }
        if (p_hashMap.containsKey("RequestSerial")) {
            serialNo = p_hashMap.get("RequestSerial");
        }
        if (p_hashMap.containsKey("SystemFlag")) {
            systemFlag = p_hashMap.get("SystemFlag");
        }
        if (p_hashMap.containsKey("NeedReturnURL")) {
            String isNeedReturnURL = p_hashMap.get("NeedReturnURL");
            if (isNeedReturnURL != null && "0".equals(isNeedReturnURL)) {
                needReturnURL = false;
            }
        }
        if (p_hashMap.containsKey("AlignCode")) {
            alignCode = p_hashMap.get("AlignCode");
        }
        if (p_hashMap.containsKey("TerminalCode")) {
            terid = p_hashMap.get("TerminalCode");
        }
        if (p_hashMap.containsKey("PaymentInstitution")) {
            paymentInstitution = p_hashMap.get("PaymentInstitution");
        }
        if (p_hashMap.containsKey("EntityLevel2")) {
            orgId = p_hashMap.get("EntityLevel2");
        }
        if (p_hashMap.containsKey("IsRaw")) {
            isRaw = p_hashMap.get("IsRaw");
        }
        if (p_hashMap.containsKey("IsLimitCredit")) {
            isLimitCredit = p_hashMap.get("IsLimitCredit");
        }
    }
}